<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          highlight-current
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini" @click="excelDAOCHU">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <!-- 表格组件 -->
        <el-table :data="employeeList">

          <el-table-column prop="staffPhoto" label="头像" width="80px" align="center">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :size="30" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />

          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment===1">正式</span>
              <span v-else-if="row.formOfEmployment===2">非正式</span>
              <span v-else>其他</span>
            </template>
          </el-table-column>

          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template>
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="queryParams.total"
            :page-size="queryParams.pagesize"
            :current-page="queryParams.page"
            @current-change="changePage"
          />
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmetnApi } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import { getEployeeApi, excleExportApi } from '@/api/employee'
import FileCoverage from 'file-saver'
export default {
  name: 'Employee',
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryParams: {
        departmentId: null,
        page: 1, // 当前第几页
        pagesize: 10, // 一页显示多少条
        total: 0 // 一共多少页
      },
      employeeList: []
    }
  },

  created() {
    this.getDepartmets()
  },

  methods: {
    // excel导出
    async excelDAOCHU() {
      // 获取二进制数据流
      const request = await excleExportApi()
      // 转换成excel格式
      FileCoverage(request, '导出的数据.xlsx')
    },

    // 页码改变触发的事件
    changePage(newPage) {
      this.queryParams.page = newPage
      this.getEmployeeList()
    },

    // 获取员工列表
    async getEmployeeList() {
      const { rows, total } = await getEployeeApi(this.queryParams)
      this.employeeList = rows
      this.queryParams.total = total
    },

    // 点击那个记录那个部门的ID
    selectNode(nodeData) {
      // 点击那个就获取那个部门的id
      this.queryParams.departmentId = nodeData.id

      // 重新点击获取页码之后重置页面要把页码重置
      this.queryParams.page = 1

      // 点击那个就获取那个部门的员工
      this.getEmployeeList()
    },

    // 获取部门负责人列表
    async getDepartmets() {
      // 转换树形结构数据赋值                 获取部门负责人列表方法
      this.depts = transListToTreeData(await getDepartmetnApi(), 0)

      // 记录传智的id
      this.queryParams.departmentId = this.depts[0].id

      // 获取默认高亮传智教育的ID
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.depts[0].id)
      })

      // 默认获取传智的
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
